package com.lizhi.rpc.handler;

import com.lizhi.common.yun.entity.RpcClientPOJO;
import com.lizhi.common.yun.entity.RpcServicePOJO;
import com.lizhi.common.yun.entity.UpDataPOJO;
import com.lizhi.common.yun.enums.HeartBeartEnum;
import com.lizhi.yun.common.enums.DataUpStateEnum;
import com.lizhi.yun.db.jlc.dao.JlcTreatrecordDao;
import com.lizhi.yun.db.jlc.pojo.JlcTreatrecordPOJO;
import com.lizhi.utils.DateUtils;
import com.lizhi.utils.StringUtils;
import com.lizhi.utils.json.SamJsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: lizhi
 * @Description:
 * @Date: create in 2020/11/2 11:17
 */
@Slf4j
@Service
public class RecvJlcTreatrecordHandler {

    @Resource
    private JlcTreatrecordDao jlcTreatrecordDao;

    /**
     * 接收同步的患者治疗信息数据
     * @param json
     * @return
     */
    public String recvJlcTreatrecord(String json){
        UpDataPOJO upPatientPOJO = SamJsonUtil.toObject(json, UpDataPOJO.class);
        RpcClientPOJO rpcClientPOJO = upPatientPOJO.getRpcClientPOJO();

        RpcServicePOJO rpcServicePOJO = new RpcServicePOJO();
        rpcServicePOJO.setSystemType(rpcClientPOJO.getSystemType());
        rpcServicePOJO.setOrganId(rpcClientPOJO.getOrganId());
        rpcServicePOJO.setBkOrganId(rpcClientPOJO.getBkOrganId());
        if (StringUtils.isEmpty(rpcClientPOJO)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000001.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000001.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }

        /*治疗信息*/
        JlcTreatrecordPOJO jlcTreatrecordPOJO = SamJsonUtil.toObject(upPatientPOJO.getData(), JlcTreatrecordPOJO.class);
        if (StringUtils.isEmpty(jlcTreatrecordPOJO)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000002.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000002.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }
        /*将医院编号和设备编号放进来，以判断是哪个设备传输的*/
        jlcTreatrecordPOJO.setHospitalid(rpcClientPOJO.getOrganId());
        jlcTreatrecordPOJO.setHospitalsystemid(rpcClientPOJO.getBkOrganId());
        /*数据上传的时间*/
        jlcTreatrecordPOJO.setDateupstatus(DataUpStateEnum.DATA_UP_STATE_01.getEnValue());
        jlcTreatrecordPOJO.setDatauptime(DateUtils.getNowDate());
        log.info("上传的数据信息是jlcTreatrecordPOJO:{}", jlcTreatrecordPOJO);

        JlcTreatrecordPOJO jlcTreatrecord = jlcTreatrecordDao.selectById(jlcTreatrecordPOJO.getId(), jlcTreatrecordPOJO.getHospitalid(), jlcTreatrecordPOJO.getHospitalsystemid());
        if (StringUtils.isEmpty(jlcTreatrecord)){
            jlcTreatrecordDao.insertRecord(jlcTreatrecordPOJO);
        } else {
            jlcTreatrecordDao.updateByIdSelective(jlcTreatrecordPOJO);
        }

        rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
        rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());
        return SamJsonUtil.toJson(rpcServicePOJO);
    }
}
